<template>
  <el-container class="page-container">
    <el-header class="page-header" height="50px">
      <div class="header-left">宏烨找房</div>
      <el-dropdown @command="handleCommand">
        <div class="header-right">{{ userInfo.name }}</div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            <el-dropdown-item command="user">用户中心</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-header>
    <el-container class="page-main">
      <el-aside class="page-menu-wrap" width="200px">
        <div class="user-role">
          {{ userRoleName }}
        </div>
        <el-menu
          active-text-color="#fff"
          background-color="#001529"
          class="page-menu"
          text-color="#aaa"
          :default-active="curRoute"
          :default-openeds="openMenus"
          router
        >
          <Menu :menuRoutes="menuRoutes"></Menu>

          <!-- <el-sub-menu index="1">
            <template #title>
              <el-icon><location /></el-icon>
              <span>Navigator One</span>
            </template>
            <el-menu-item-group title="Group One">
              <el-menu-item index="1-1">item one</el-menu-item>
              <el-menu-item index="1-2">item two</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="Group Two">
              <el-menu-item index="1-3">item three</el-menu-item>
            </el-menu-item-group>
            <el-sub-menu index="1-4">
              <template #title>item four</template>
              <el-menu-item index="1-4-1">item one</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>
          <el-menu-item index="2">
            <el-icon><icon-menu /></el-icon>
            <span>Navigator Two</span>
          </el-menu-item>
          <el-menu-item index="3" disabled>
            <el-icon><document /></el-icon>
            <span>Navigator Three</span>
          </el-menu-item>
          <el-menu-item index="4">
            <el-icon><setting /></el-icon>
            <span>Navigator Four</span>
          </el-menu-item> -->
        </el-menu>
      </el-aside>
      <el-main>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item
            v-for="item in breadCrumbArr"
            :key="item.path"
            :to="{ path: item.path }"
            >{{ item.name }}</el-breadcrumb-item
          >
          <!-- <el-breadcrumb-item>
            <a href="/">promotion management</a>
          </el-breadcrumb-item>
          <el-breadcrumb-item>promotion list</el-breadcrumb-item>
          <el-breadcrumb-item>promotion detail</el-breadcrumb-item> -->
        </el-breadcrumb>
        <RouterView></RouterView>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, watch, toRefs } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { menuRoutes } from '@/router/index'
import Menu from '@/components/Menu.vue'
import useUser from '@/stores/useUser'
const route = useRoute()
const router = useRouter()
const { userInfo } = toRefs(useUser())
const curRoute = ref(route.path)
const openMenu = route.matched.map((item) => item.path) //map,filter,find,findIndex,forEach,some,every
const openMenus = ref(openMenu)
// const breadCrumbArr = computed(() =>
//   route.matched.map((item) => ({ name: item.name, path: item.path })).slice(1)
// ) //map,filter,find,findIndex,forEach,some,every
// const userName = ref('lishaobin')
const breadCrumbArr = ref()
watch(
  route,
  (newVal) => {
    breadCrumbArr.value = newVal.matched
      .map((item) => ({ name: item.name, path: item.path }))
      .slice(1)
  },
  { immediate: true }
)
const userRoleName = ref('管理员')
const handleCommand = (val: string) => {
  console.log(val)
  if (val === 'logout') {
    localStorage.removeItem('token')
    router.push('/login')
  }
}
</script>

<style lang="scss" scoped>
.page-container {
  height: 100%;
}
.page-header {
  background: #4382e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.page-main {
  height: calc(100% - 50px);
}
.page-menu {
  border-color: #001529;
}
.page-menu-wrap {
  background: #001529;
}
.user-role {
  height: 40px;
  line-height: 40px;
  text-align: center;
  color: #ccc;
}
</style>
